package acwing.算法题;/**
 * @author： li
 * @date： 2022-04-04 21:13
 * @version 1.0
 */
import jdk.internal.util.xml.impl.Pair;

import java.io.*;
import java.util.*;
public class 超市_并查集 {
    static class h{
        private int p;
        private int d;

        public h(int p, int d) {
            this.p = p;
            this.d = d;
        }
    }
    static Scanner sc =new Scanner(new InputStreamReader(System.in));
    static int[] fa = new int[10005];
    static int find(int x){
        if(fa[x]==x)
            return x;
        return fa[x]=find(fa[x]);
    }
    static void join(int x,int y){
        int fx =find(x);int fy=find(y);
        if(fx!=fy)
            fa[fx]=fy;
    }
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws InterruptedException, IOException {
        while (sc.hasNext()){
            h[] pre =new h[10005];
            int deadline=0;long ans=0;
            int n =sc.nextInt();
            if(n==0)
                System.out.println(0);
            else {
                    for(int i=1;i<=n;i++){
                        int p=sc.nextInt();
                        int d =sc.nextInt();
                         pre[i]=new h(p,d);
                        deadline=Math.max(deadline,d);
                    }
                    Arrays.sort(pre,1,n+1, Comparator.comparingInt(o -> o.p));
                    for(int i=1;i<=deadline;i++)
                        fa[i]=i;
                    for(int i=n;i>=1;i--){
                        int r =find(pre[i].d);
                        if(r>0){
                            ans+=pre[i].p;
                            fa[r]=r-1;
                        }
                    }
                    out.write(ans+"\n");
                    Arrays.fill(fa,0);

            }
            Arrays.fill(fa,0);
        }
        out.flush();
    }
}
